﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FastReflectionLib;
using System.Web;
using System.Data;
using SZHL.UI.AppCode;
using SZHL.Data;
using Newtonsoft.Json;

namespace SZHL.UI.API
{
    public class KHGLManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(KHGLManage).GetMethod(msg.Action.ToUpper());
            KHGLManage model = new KHGLManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        /// <summary>
        /// 我的客户列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="strParamData"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETUSERKHGLDATA(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            DataTable dtList = new DataTable();
            string strWhere = string.Format("  where ','+KHFXRS+','  like '%," + UserInfo.User.UserName + ",%'");
            if (P1 != "")
            {
                strWhere += string.Format(" And  KHLeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And ( KHName like '%{0}%' or KHDW like '%{0}%' or KHPhone like '%{0}%' )", P2);
            }
            string branchCode = context.Request.QueryString["b"] ?? "";
            if (branchCode != "")
            {
                strWhere += string.Format(" And  u.BranchCode={0}", branchCode);
            }
            string strSql = " select kh.ID,kh.KHName,kh.KHLeiBie,kh.KHDW,kh.KHPhone,kh.KHAdress,kh.KHLY,u.BranchCode,KHXinBie,KHZhiWu,KHFXRS,kh.Remark,kh.CRDate from  ZH_KHGL kh inner join JH_Auth_User u on kh.CRUser=u.UserName ";
            dtList = new ZH_KHGLB().GetDTByCommand(strSql + strWhere);
            msg.Result = dtList;
        }
        /// <summary>
        /// 我的客户列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="strParamData"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETKHGLDATA(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            DataTable dtList = new DataTable();
            string strWhere = string.Format("where  1=1");
            if (P1 != "")
            {
                strWhere += string.Format(" And  KHLeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And ( KHName like '%{0}%' or KHDW like '%{0}%' or KHPhone like '%{0}%' )", P2);
            }
            string branchCode = context.Request.QueryString["b"] ?? "";
            if (branchCode != "")
            {
                strWhere += string.Format(" And  u.BranchCode={0}", branchCode);
            }
            string strSql = " select kh.ID,kh.KHName,kh.KHLeiBie,kh.KHDW,kh.KHPhone,kh.KHAdress,kh.KHLY,u.BranchCode,KHXinBie,KHZhiWu,KHFXRS,kh.Remark,kh.CRDate from  ZH_KHGL kh inner join JH_Auth_User u on kh.CRUser=u.UserName ";
            dtList = new ZH_KHGLB().GetDTByCommand(strSql + (string.IsNullOrEmpty(P1 + P2 + branchCode) ? "" : strWhere));
            dtList.Columns.Add("BranchName");
            foreach (DataRow row in dtList.Rows)
            {
                row["BranchName"] = new JH_Auth_BranchB().GetBranchNameByCode(int.Parse(row["BranchCode"].ToString()));
            }
            msg.Result = dtList;
        }
        /// <summary>
        /// 添加客户管理
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">客户信息</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDKHGL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_KHGL khgl = JsonConvert.DeserializeObject<ZH_KHGL>(P1);
            if (khgl.KHName.Trim() == "")
            {
                msg.ErrorMsg = "客户名称不能为空";
                return;
            }
            if (khgl.KHDW.Trim() == "")
            {
                msg.ErrorMsg = "客户单位不能为空";
                return;
            }

            if (khgl.ID == 0)
            {
                khgl.CRDate = DateTime.Now;
                khgl.CRUser = UserInfo.User.UserName;
                new ZH_KHGLB().Insert(khgl);
            }
            else
            {
                new ZH_KHGLB().Update(khgl);
            }
            msg.Result = khgl;
        }
        /// <summary>
        /// 按照分页查询
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">客户类别</param>
        /// <param name="P2">查询条件</param>
        /// <param name="UserInfo"></param>
        public void GEKHGLDATAPAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = "  1=1 ";
            if (P1 != "")
            {
                strWhere += string.Format(" And  KHLeiBie='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And ( KHName like '%{0}%' or KHDW like '%{0}%' or KHPhone like '%{0}%' )", P2);
            }
            //部门
            string branchCode = context.Request.QueryString["b"] ?? "";
            if (branchCode != "")
            {
                strWhere += string.Format(" And  u.BranchCode={0}", branchCode);
            }
            //页码
            int page = 0;
            int.TryParse(context.Request.QueryString["p"], out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            DataTable dtList = new ZH_KHGLB().GetDataPager(" ZH_KHGL kh inner join JH_Auth_User u on kh.CRUser=u.UserName ", "kh.ID,kh.KHName,kh.KHLeiBie,kh.KHDW,kh.KHPhone,kh.KHAdress,kh.KHLY,u.BranchCode,KHXinBie,KHZhiWu,KHFXRS,kh.Remark", 8, page, "CRDate desc", strWhere, ref total);
            dtList.Columns.Add("BranchName");
            foreach (DataRow row in dtList.Rows)
            {
                row["BranchName"] = new JH_Auth_BranchB().GetBranchNameByCode(int.Parse(row["BranchCode"].ToString()));
            }
            msg.Result = dtList;
        }
        /// <summary>
        /// 获取客户信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">客户信息ID</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETKHGLMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new ZH_KHGLB().GetEntity(d => d.ID.ToString() == P1);
        }
        /// <summary>
        /// 删除客户管理
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">客户管理Id</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELKHGLBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                if (new ZH_KHGLB().Delete(d => d.ID.ToString() == P1))
                {
                    msg.ErrorMsg = "";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
    }
}